home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTdoc.lha / docs / doc2tex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-30  |  5.7 KB  |  279 lines

  1. /*
  2.  * doc2tex.c  -- program to convert Gnuplot .DOC format to LaTeX document
  3.  * Also will work for VMS .HLP files. 
  4.  * Modified by Russell Lang from hlp2ms.c by Thomas Williams 
  5.  * Extended by David Kotz to support quotes ("), backquotes, tables.
  6.  *
  7.  * usage:  doc2tex [file.doc [file.tex]]
  8.  *
  9.  *   where file.doc is a Gnuplot .DOC file, and file.tex will be an
  10.  *     article document suitable for printing with LaTeX.
  11.  *
  12.  * typical usage for GNUPLOT:
  13.  *
  14.  *   doc2tex gnuplot.doc gnuplot.tex 
  15.  *   latex gnuplot.tex ; latex gnuplot.tex
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20.  
  21. #include "ansichek.h"
  22.  
  23. #ifdef ANSI_C
  24. #include <stdlib.h>
  25. #endif
  26.  
  27.  
  28. #define MAX_NAME_LEN    256
  29. #define MAX_LINE_LEN    256
  30. #define TRUE 1
  31. #define FALSE 0
  32.  
  33. void main __P(( int argc, char **argv ));
  34. void init __P(( FILE *b ));
  35. void convert __P(( FILE *a, FILE *b ));
  36. void process_line __P(( char *line, FILE *b, unsigned int linecounter ));
  37. void section __P(( char *line, FILE *b ));
  38. void puttex __P(( char *str, FILE *file ));
  39. void finish __P(( FILE *b ));
  40.  
  41.  
  42. typedef int boolean;
  43.  
  44. boolean intable = FALSE;
  45. boolean verb = FALSE;
  46.  
  47. void main(argc,argv)
  48.     int argc;
  49.     char **argv;
  50. {
  51.     FILE * infile;
  52.     FILE * outfile;
  53.  
  54.     infile = stdin;
  55.     outfile = stdout;
  56.     if (argc > 3) {
  57.         fprintf(stderr,"Usage: %s [infile [outfile]]\n", argv[0]);
  58.         exit(1);
  59.     }
  60.     if (argc >= 2) 
  61.         if ( (infile = fopen(argv[1],"r")) == (FILE *)NULL) {
  62.             fprintf(stderr,"%s: Can't open %s for reading\n",
  63.                 argv[0], argv[1]);
  64.             exit(1);
  65.         }
  66.     if (argc == 3)
  67.         if ( (outfile = fopen(argv[2],"w")) == (FILE *)NULL) {
  68.             fprintf(stderr,"%s: Can't open %s for writing\n",
  69.                 argv[0], argv[2]);
  70.         }
  71.     
  72.     init(outfile);
  73.     convert(infile,outfile);
  74.     finish(outfile);
  75.     exit(0);
  76. }
  77.  
  78.  
  79. void init(b)
  80.     FILE *b;
  81. {
  82.     (void) fputs("\\input{titlepag.tex}\n",b);
  83. }
  84.  
  85.  
  86. void convert(a,b)
  87.     FILE *a,*b;
  88. {
  89.     static char line[MAX_LINE_LEN];
  90.     unsigned int linecounter=0;
  91.  
  92.     while (fgets(line,MAX_LINE_LEN,a)) {
  93.        process_line(line, b,++linecounter);
  94.     }
  95. }
  96.  
  97. void process_line(line, b,linecounter)
  98.     char *line;
  99.     FILE *b;
  100.     unsigned int linecounter;
  101. {
  102.     switch(line[0]) {        /* control character */
  103.        case '?': {            /* interactive help entry */
  104.           break;            /* ignore */
  105.        }
  106.        case '@': {            /* start/end table */
  107.           if (intable) {
  108.              (void) fputs("\\hline\n\\end{tabular}\n", b);
  109.              (void) fputs("\\end{center}\n",b);
  110.              intable = FALSE;
  111.           } else {
  112.              if (verb) {
  113.                 (void) fputs("\\end{verbatim}\n",b);
  114.                 verb=FALSE;
  115.              } 
  116.              (void) fputs("\n\\begin{center}\n", b);
  117.              (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
  118.              intable = TRUE;
  119.           }
  120.           /* ignore rest of line */
  121.           break;
  122.        }
  123.        case '#': {            /* latex table entry */
  124.           if (intable)
  125.             (void) fputs(line+1, b); /* copy directly */
  126.           else{
  127.             fprintf(stderr, "warning: # line found outside of table\n");
  128.             fprintf(stderr, "%s\n",line+1);}
  129.  
  130.           break;
  131.        }
  132.        case '^': {            /* external link escape */
  133.           break;            /* ignore */
  134.        }
  135.        case '%': {            /* troff table entry */
  136.           break;            /* ignore */
  137.        }
  138.        case '\n':            /* empty text line */
  139.        case ' ': {            /* normal text line */
  140.           if (intable)
  141.             break;        /* ignore while in table */
  142.           if (line[1] == ' ') {
  143.              /* verbatim mode */
  144.              if (!verb) {
  145.                 (void) fputs("\\begin{verbatim}\n",b);
  146.                 verb=TRUE;
  147.              }
  148.              (void) fputs(line+1,b); 
  149.           } else {
  150.              if (verb) {
  151.                 (void) fputs("\\end{verbatim}\n",b);
  152.                 verb=FALSE;
  153.              } 
  154.              if (line[0] == '\n')
  155.                puttex(line,b); /* handle totally blank line */
  156.              else
  157.                puttex(line+1,b);
  158.           }
  159.           break;
  160.        }
  161.        default: {
  162.           if (isdigit(line[0])) { /* start of section */
  163.              if (!intable)    /* ignore while in table */
  164.                section(line, b);
  165.           } else
  166.             fprintf(stderr, 
  167.                 "unknown control code '%c' in column 1\n line:%d: %s", 
  168.                   line[0],linecounter,line);
  169.           break;
  170.        }
  171.     }
  172. }
  173.  
  174. /* process a line with a digit control char */
  175. /* starts a new [sub]section */
  176.  
  177. void section(line, b)
  178.     char *line;
  179.     FILE *b;
  180. {
  181.     static char string[MAX_LINE_LEN];
  182.     int sh_i;
  183.  
  184.     if (verb) {
  185.        (void) fputs("\\end{verbatim}\n",b);
  186.        verb=FALSE;
  187.     } 
  188.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  189.     switch(sh_i)
  190.      {
  191.         case 1: 
  192.         (void) fprintf(b,"\\section{");
  193.         break;
  194.         case 2: 
  195.         (void) fprintf(b,"\\section{");
  196.         break;
  197.         case 3:
  198.         (void) fprintf(b,"\\subsection{");
  199.         break;
  200.         case 4: 
  201.         (void) fprintf(b,"\\subsubsection{");
  202.         break;
  203.         default:
  204.         case 5: 
  205.         (void) fprintf(b,"\\paragraph{");
  206.         break;
  207.      }
  208.     if (islower(string[0]))
  209.      string[0] = toupper(string[0]);
  210.     puttex(string,b);
  211.     (void) fprintf(b,"}\n");
  212. }
  213.  
  214. /* put text in string str to file while buffering special TeX characters */
  215. void puttex(str,file)
  216.     FILE *file;
  217.     register char *str;
  218. {
  219.     register char ch;
  220.     static boolean inquote = FALSE;
  221.  
  222.      while( (ch = *str++) != '\0') {
  223.          switch(ch) {
  224.              case '#':
  225.              case '$':
  226.              case '%':
  227.              case '&':
  228.              case '_':
  229.              case '{':
  230.              case '}':
  231.                  (void) fputc('\\',file);
  232.                  (void) fputc(ch,file);
  233.                  break;
  234.              case '\\':
  235.                  (void) fputs("$\\backslash$",file);
  236.                  break;
  237.              case '~':
  238.                  (void) fputs("\\~{\\ }",file);
  239.                  break;
  240.              case '^':
  241.                  (void) fputs("\\verb+^+",file);
  242.                  break;
  243.              case '>':
  244.              case '<':
  245.              case '|':
  246.                  (void) fputc('$',file);
  247.                  (void) fputc(ch,file);
  248.                  (void) fputc('$',file);
  249.                  break;
  250.              case '"': 
  251.                  /* peek at next character: if space, end of quote */
  252.                  if (*str == '\0' || isspace(*str) || ispunct(*str))
  253.                    (void) fputs("''", file);
  254.                  else
  255.                    (void) fputs("``", file);
  256.                  break;
  257.              case '`':    /* backquotes mean boldface */
  258.                  if (inquote) {
  259.                     fputs("}", file);
  260.                     inquote = FALSE;
  261.                  } else {
  262.                     fputs("{\\bf ", file);
  263.                     inquote = TRUE;
  264.                  }
  265.                  break;
  266.              default:
  267.                  (void) fputc(ch,file);
  268.                  break;
  269.          }
  270.      }
  271. }
  272.  
  273.  
  274. void finish(b)
  275.     FILE *b;
  276. {
  277.     (void) fputs("\\end{document}\n",b);
  278. }
  279.